﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using Wjtour.Common;

namespace Wjtour.BLL
{
    public class resturant_bll : base_dll
    {
        public List<Wjtour.Domain.resturant> GetList(int top)
        {
            string sql = string.Format("select top {0} * from resturant order by isrecommend desc, id desc", top);
            return GetAll<Wjtour.Domain.resturant>(sql).ToList();
        }


        public PaginationData<Wjtour.Domain.resturant> GetList(string q, int page, int pagesize = 20)
        {
            string where = " where 1=1";
            if (!string.IsNullOrEmpty(q))
                where += " and resturantname like '%" + Utility.GetSafeSql(q) + "%'";
            string sql = string.Format(@"select * from resturant 
                {0} 
                order by isrecommend desc, id desc", where);
            PaginationData<Wjtour.Domain.resturant> data = GetPaginationData<Wjtour.Domain.resturant>(sql, page, pagesize);
            return data;
        }

        public Wjtour.Domain.resturant Load(int id)
        {
            string sql = "select * from resturant where id=" + id;
            return DbAccess.GetObjects<Wjtour.Domain.resturant>(sql).FirstOrDefault();
        }

        public bool Save(Wjtour.Domain.resturant entity)
        {
            string sql = "";

            DbCommand cmd = DbAccess.CreateCommand();

            if (entity.id > 0)
            {
                sql = @"UPDATE [resturant] SET 
                [resturantname] = @resturantname,
                [img] = @img,
                [address] = @address,
                [opentime] = @opentime,
                [contactphone] = @contactphone,
                [holdmans] = @holdmans,
                [averagecosume] = @averagecosume,
                [trafficguide] = @trafficguide,
                [introduce] = @introduce,
                [addtime] = @addtime,
                isrecommend = @isrecommend,
                web = @web
                 WHERE id=@id";
                DbAccess.AddInParameter(cmd, "@id", DbType.Int32, entity.id);

            }
            else
            {
                sql = @"INSERT INTO [resturant]( 
                [resturantname],[img],[address],[opentime],[contactphone],[holdmans],[averagecosume],[trafficguide],[introduce],[addtime],isrecommend,web)VALUES(
                @resturantname,@img,@address,@opentime,@contactphone,@holdmans,@averagecosume,@trafficguide,@introduce,@addtime,@isrecommend,@web)";
            }
            DbAccess.AddInParameter(cmd, "@resturantname", DbType.String, entity.resturantname);
            DbAccess.AddInParameter(cmd, "@img", DbType.String, entity.img);
            DbAccess.AddInParameter(cmd, "@address", DbType.String, entity.address);
            DbAccess.AddInParameter(cmd, "@opentime", DbType.String, entity.opentime);
            DbAccess.AddInParameter(cmd, "@contactphone", DbType.String, entity.contactphone);
            DbAccess.AddInParameter(cmd, "@holdmans", DbType.String, entity.holdmans);
            DbAccess.AddInParameter(cmd, "@averagecosume", DbType.String, entity.averagecosume);
            DbAccess.AddInParameter(cmd, "@trafficguide", DbType.String, entity.trafficguide);
            DbAccess.AddInParameter(cmd, "@introduce", DbType.String, entity.introduce);
            DbAccess.AddInParameter(cmd, "@addtime", DbType.DateTime, entity.addtime);
            DbAccess.AddInParameter(cmd, "@isrecommend", DbType.Int32, entity.isrecommend);
            DbAccess.AddInParameter(cmd, "@web", DbType.String, entity.web);
            cmd.CommandText = sql;
            cmd.CommandType = System.Data.CommandType.Text;
            return DbAccess.ExecuteNonQuery(cmd) > 0;

        }


        public int BatchDelete(string ids)
        {
            string sql = "delete from resturant where id in(" + ids + ")";
            return DbAccess.ExecuteNonQuery(sql);
        }
    }
}
